class Solution(object):
    def insert(self, intervals, newInterval):
        """
        :type intervals: List[List[int]]
        :type newInterval: List[int]
        :rtype: List[List[int]]
        """
        l_index = -1
        l_flag = 0
        r_index = len(intervals)
        r_flag = 0
        return_list = []
        for i,interval in enumerate(intervals):
            if newInterval[0] >= interval[0]:
                l_index = i
                if newInterval[0] <= interval[1]:
                    l_flag = 1
        for i, interval in enumerate(intervals):
            if newInterval[1] <= interval[1]:
                r_index = i
                if newInterval[1] >= interval[0]:
                    r_flag = 1
                break
        if len(intervals) > 0 and r_index == -1:
            r_index = len(intervals) + 1
        print(l_index, r_index, l_flag, r_flag)
        flag = 0
        for i, interval in enumerate(intervals):
            if i < l_index or i > r_index:
                return_list.append(interval)
            else:
                if flag == 0:
                    if l_flag == 0:
                        if l_index != -1:
                            return_list.append(intervals[l_index])
                        if r_flag == 0:
                            return_list.append(newInterval)
                            if r_index < len(intervals):
                                return_list.append(intervals[r_index])
                        else:
                            return_list.append([newInterval[0], intervals[r_index][1]])
                    if l_flag == 1:
                        if r_flag == 0:
                            return_list.append([intervals[l_index][0], newInterval[1]])
                            if r_index < len(intervals):
                                return_list.append(intervals[r_index])
                        else:
                            return_list.append([intervals[l_index][0], intervals[r_index][1]])
                    flag = 1
        if len(return_list) == 0:
            return_list.append(newInterval)
        return return_list


intervals = [[1, 5]]
newInterval = [0,3]
data = Solution()
print(data.insert(intervals, newInterval))
